package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.topscore.uke.R;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.fragments.CouponInCommissionFragment;
import com.topscore.uke.ui.fragments.CouponInExpiredFragment;
import com.topscore.uke.ui.fragments.CouponInOccupiedFragment;
import com.topscore.uke.ui.fragments.CouponInUsedFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyCouponActivity extends ToolBarCompatActivity {

    @Bind(R.id.tab_layout)
    CommonTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private final String[] _TabTitles = {"可使用", "已使用", "已占用", "已过期"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        ButterKnife.bind(this);
        setCentralTitle("我的卡券");
        initViews();
    }

    private void initViews() {
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < _TabTitles.length; i++) {
            final String title = _TabTitles[i];
            CustomTabEntity entity = new CustomTabEntity() {
                @Override
                public String getTabTitle() {
                    return title;
                }

                @Override
                public int getTabSelectedIcon() {
                    return 0;
                }

                @Override
                public int getTabUnselectedIcon() {
                    return 0;
                }
            };
            tabEntities.add(entity);
        }
        tabLayout.setTabData(tabEntities);
        viewPager.setAdapter(new ViewPagerAdapter());
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList;

        public ViewPagerAdapter() {
            super(getSupportFragmentManager());
            mFragmentList = new ArrayList<>();
            mFragmentList.add(new CouponInCommissionFragment());
            mFragmentList.add(new CouponInUsedFragment());
            mFragmentList.add(new CouponInOccupiedFragment());
            mFragmentList.add(new CouponInExpiredFragment());
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}
